package cloud.lysky.java.on.concurrent.thread;

/**
 * @author luoly
 * @describe
 * @date 2022-10-18 19:11
 **/
public class VolatileDemo {
    private static volatile boolean flag = true;

    public static void main(String[] args) {
        //假设线程A
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    if (flag){
                        System.out.println("线程A: 灯是开着的，关掉");
                        flag = false;
                    }
                }
            }
        }).start();
        //假设线程B
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    if (!flag){
                        System.out.println("线程B: 灯是关着的，打开");
                        flag = true;
                    }
                }
            }
        }).start();
    }
}